<template>
  <div class="tree-node">
    <div 
      class="node-content" 
      :class="{ 
        'selected': selectedId === node.distributorId,
        'has-children': node.children && node.children.length > 0,
        'inactive': node.status !== 1
      }"
      @click="handleNodeClick"
    >
      <!-- 展开/收起按钮 -->
      <div class="expand-btn" @click.stop="handleToggle" v-if="node.children && node.children.length > 0">
        <svg 
          class="expand-icon" 
          :class="{ 'expanded': isExpanded }"
          viewBox="0 0 24 24"
        >
          <path d="M8.59,16.58L13.17,12L8.59,7.41L10,6L16,12L10,18L8.59,16.58Z"/>
        </svg>
      </div>
      <div class="expand-placeholder" v-else></div>

      <!-- 分销员信息 -->
      <div class="distributor-info">
        <div class="distributor-avatar">
          <img :src="node.avatar || '/default-avatar.png'" :alt="node.nickname">
        </div>
        <div class="distributor-details">
          <div class="distributor-name">{{ node.nickname }}</div>
          <div class="distributor-meta">
            <span class="distributor-code">{{ node.distributorCode }}</span>
            <span class="level-badge" :class="'level' + node.level">
              {{ node.level }}级
            </span>
          </div>
        </div>
      </div>

      <!-- 统计信息 -->
      <div class="node-stats">
        <div class="stat-item">
          <span class="stat-label">下级</span>
          <span class="stat-value">{{ node.directSubCount || 0 }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">佣金</span>
          <span class="stat-value commission">￥{{ formatCurrency(node.commissionBalance || 0) }}</span>
        </div>
      </div>

      <!-- 状态指示器 -->
      <div class="status-indicator" :class="node.status === 1 ? 'active' : 'inactive'"></div>
    </div>

    <!-- 子节点 -->
    <div v-if="isExpanded && node.children && node.children.length > 0" class="children">
      <DistributorTreeNode
        v-for="child in node.children"
        :key="child.distributorId"
        :node="child"
        :selected-id="selectedId"
        :expanded-nodes="expandedNodes"
        @node-click="$emit('node-click', $event)"
        @node-toggle="$emit('node-toggle', $event)"
      />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

// Props
const props = defineProps({
  node: {
    type: Object,
    required: true
  },
  selectedId: {
    type: [String, Number],
    default: null
  },
  expandedNodes: {
    type: Set,
    required: true
  }
})

// Emits
const emit = defineEmits(['node-click', 'node-toggle'])

// 计算属性
const isExpanded = computed(() => {
  return props.expandedNodes.has(props.node.distributorId)
})

// 事件处理
const handleNodeClick = () => {
  emit('node-click', props.node)
}

const handleToggle = () => {
  emit('node-toggle', props.node.distributorId)
}

// 工具函数
const formatCurrency = (amount) => {
  return Number(amount).toLocaleString('zh-CN', { minimumFractionDigits: 0, maximumFractionDigits: 0 })
}
</script>

<style scoped>
.tree-node {
  position: relative;
}

.node-content {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
  background: white;
  margin-bottom: 4px;
}

.node-content:hover {
  background: #f8fafc;
  border-color: #e2e8f0;
}

.node-content.selected {
  background: #eef2ff;
  border-color: #667eea;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
}

.node-content.inactive {
  opacity: 0.6;
}

.node-content.inactive .distributor-name {
  text-decoration: line-through;
  color: #9ca3af;
}

.expand-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
}

.expand-btn:hover {
  background: #e2e8f0;
}

.expand-placeholder {
  width: 24px;
  margin-right: 8px;
}

.expand-icon {
  width: 16px;
  height: 16px;
  fill: #64748b;
  transition: transform 0.2s;
}

.expand-icon.expanded {
  transform: rotate(90deg);
}

.distributor-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.distributor-avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.distributor-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.distributor-details {
  flex: 1;
  min-width: 0;
}

.distributor-name {
  font-size: 0.9rem;
  font-weight: 500;
  color: #2d3748;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.distributor-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.distributor-code {
  font-size: 0.75rem;
  color: #64748b;
  font-family: monospace;
}

.level-badge {
  padding: 2px 6px;
  border-radius: 8px;
  font-size: 0.7rem;
  font-weight: 600;
}

.level-badge.level1 {
  background: #fef3c7;
  color: #92400e;
}

.level-badge.level2 {
  background: #e0e7ff;
  color: #5b21b6;
}

.level-badge.level3 {
  background: #d1fae5;
  color: #065f46;
}

.node-stats {
  display: flex;
  gap: 16px;
  margin-right: 8px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 40px;
}

.stat-label {
  font-size: 0.7rem;
  color: #64748b;
  margin-bottom: 2px;
}

.stat-value {
  font-size: 0.8rem;
  font-weight: 600;
  color: #2d3748;
}

.stat-value.commission {
  color: #059669;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-indicator.active {
  background: #10b981;
}

.status-indicator.inactive {
  background: #ef4444;
}

.children {
  margin-left: 32px;
  border-left: 1px dashed #e2e8f0;
  padding-left: 16px;
  margin-top: 4px;
}

/* 连接线样式 */
.children::before {
  content: '';
  position: absolute;
  left: 32px;
  top: -4px;
  width: 16px;
  height: 1px;
  background: #e2e8f0;
}

.children .tree-node:last-child {
  position: relative;
}

.children .tree-node:last-child::after {
  content: '';
  position: absolute;
  left: -17px;
  top: 50%;
  bottom: 0;
  width: 1px;
  background: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .node-content {
    padding: 10px 12px;
  }
  
  .node-stats {
    gap: 12px;
  }
  
  .stat-item {
    min-width: 35px;
  }
  
  .children {
    margin-left: 20px;
    padding-left: 12px;
  }
}
</style> 